/**
 * Copyright (c) 2011-2012, James Zhan 詹波 (jfinal@126.com).
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.jfinal.util;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.channels.FileChannel;

class FileUtil
{
	public static void copyDirectiory(String source, String dest) throws IOException {
		(new File(dest)).mkdirs();
		File[] file = (new File(source)).listFiles();
		for (File o : file) {
			if (o.isFile()) {
				FileInputStream input = new FileInputStream(o);
				FileOutputStream output = new FileOutputStream(dest + "/" + o.getName());
				byte[] b = new byte[1024 * 5];
				int len;
				while ((len = input.read(b)) != -1) {
					output.write(b, 0, len);
				}
				output.flush();
				output.close();
				input.close();
			}
			if (o.isDirectory()) {
				copyFunction(dest + "/" + o.getName(), source + "/" + o.getName());
			}
		}
	}
	
	private static void copyFunction(String source, String dest) throws IOException {
		(new File(source)).mkdirs();
		File[] file = (new File(dest)).listFiles();
		for (File o : file) {
			if (o.isFile()) {
				FileInputStream input = new FileInputStream(o);
				FileOutputStream output = new FileOutputStream(source + "/" + o.getName());
				byte[] b = new byte[1024 * 5];
				int len;
				while ((len = input.read(b)) != -1) {
					output.write(b, 0, len);
				}
				output.flush();
				output.close();
				input.close();
			}
			if (o.isDirectory()) {
				copyFunction(source + "/" + o.getName(), dest + "/" + o.getName());
			}
		}
	}
	
	public static boolean copyFile(File sourceFile, File destFile) {
		try {
			FileInputStream input = new FileInputStream(sourceFile);
			FileOutputStream output = new FileOutputStream(destFile);
			byte[] b = new byte[1024 * 5];
			int len;
			while ((len = input.read(b)) != -1) {
				output.write(b, 0, len);
			}
			output.flush();
			output.close();
			input.close();
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
		return true;
	}
	
	public void delAll(File f) throws IOException {
		if (!f.exists())
			throw new IOException("指定目录不存在:" + f.getName());
		boolean rslt = true;
		if (!(rslt = f.delete())) {
			File subs[] = f.listFiles();
			for (int i = 0; i <= subs.length - 1; i++) {
				if (subs[i].isDirectory())
					delAll(subs[i]);
				rslt = subs[i].delete();
			}
			rslt = f.delete();
		}
		if (!rslt)
			throw new IOException("无法删除:" + f.getName());
		return;
	}
	
	public static boolean copyFileUseFileChanne(String srcFilename, String dstFilename) {
		FileChannel srcChannel = null;
		FileChannel dstChannel = null;
		try {
		    srcChannel = new FileInputStream(srcFilename).getChannel();
		    dstChannel = new FileOutputStream(dstFilename).getChannel();
		    dstChannel.transferFrom(srcChannel, 0, srcChannel.size());
		    return true;
		} catch (IOException e) {
			e.printStackTrace();
			return false;
		}
		finally {
			try { srcChannel.close(); } catch (Exception e) { e.printStackTrace(); }
		    try { dstChannel.close(); } catch (Exception e) { e.printStackTrace(); }
		}
	}
}




